package gen.core.others;

import gen.core.utils.NameUtils;

import java.beans.PropertyDescriptor;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;

import org.apache.commons.dbutils.BeanProcessor;

/**
 * 扩展BeanProcessor的处理方式，使其能够处理如DATA_OBJECT_NAME -> dataObjectName这样的映射关系
 * 
 * @author Gu Lei
 */
public class GenBeanProcess extends BeanProcessor {
	/**
	 * 替换BeanProcessor的映射关系处理
	 */
	@Override
	protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException {

		int cols = rsmd.getColumnCount();
		int[] columnToProperty = new int[cols + 1];
		Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);

		for (int col = 1; col <= cols; col++) {
			String columnName = rsmd.getColumnLabel(col);
			if (null == columnName || 0 == columnName.length()) {
				columnName = rsmd.getColumnName(col);
			}

			for (int i = 0; i < props.length; i++) {
				if (NameUtils.convert1(columnName).equals(props[i].getName())) {
					columnToProperty[col] = i;
					break;
				}
			}
		}

		return columnToProperty;
	}
}
